/*
project: lightModal
version: (12.02.2009)
author: Jamison Morrow
project home: http://code.google.com/p/light-modal/
license: MIT, http://www.opensource.org/licenses/mit-license.php
*/

var lightModal = { loadingImage: "http://light-modal.googlecode.com/files/loading.gif" };

//SHOW A CENTERED MODAL DIV
lightModal.show = function(params)
{
    //hide flash
    var visibleFlash = $("object:visible,embed:visible").addClass("__visibleFlash");
    $(".__visibleFlash").hide();

    //kill scrolling
    var body = $("body");
    body.css("overflow", "hidden");

    //modal count
    var modalCount = $(".lightModal_modal_container").length;

    //modal container
    var modalContainer = $("<div class='lightModal_modal_container'/>");

    //show modal
    var modal = $("<div class='lightModal_modal' style='overflow:auto;padding: 20px;position: fixed;left: 0;top: 0;background: #fff;border: 3px solid #222;'/>");
    modal.width(params.width);
    modal.height(params.height);
    modal.css("z-index", modalCount * 1000 + 1);
    modal.css("top", ($(document).height() - params.height - 50) / 2);
    modal.css("left", ($(document).width() - params.width - 20) / 2);
    modalContainer.append(modal);

    //show overlay
    var overlay = $("<div class='lightModal_modal_overlay' style='position: fixed;top: 0;left: 0;width: 100%;height: 100%;background: #222;' />");
    overlay.css('opacity', 0.7);
    overlay.css("filter", "alpha(opacity=70)");
    overlay.click(function() { lightModal.remove(modalContainer) });
    overlay.css("z-index", modalCount * 1000);
    modalContainer.append(overlay);

    //add all to container
    $(body).append(modalContainer);

    //ajax call (if no html)
    if (!params.html)
    {
        //show loading image
        var loadingImage = $('<img src="' + lightModal.loadingImage + '"/>');
        modal.append(loadingImage);

        //ajax call
        $.ajax({
            type: "GET",
            url: params.url,
            success: function(html)
            {
                modal.html(html);
                $(".lightModal_focus").eq(0).focus();
            },
            error: function(xhr, text, error)
            {
                lightModal.alert({ html: xhr.responseText, width: 600, height: 400, callBack: function() { lightModal.remove(modalContainer) } })
            }
        });
    }
    else
    {
        modal.append(params.html);
    }

    return false;
};

//REMOVES A MODAL
lightModal.remove = function(elements)
{
    //kill modals
    elements = elements || $(".lightModal_modal_container");
    elements.remove();

    //revive scrolling
    var body = $("body");
    body.css("overflow", "auto");

    //show flash
    $(".__visibleFlash").show().removeClass("__visibleFlash");

    //focus
    $(".lightModal_focus").focus();
};

//CLOSES THE CURRENT PARENT MODAL
lightModal.closeParentModal = function(element)
{
    lightModal.remove(element.parents(".lightModal_modal_container"));
};

//SHOWS AN ALERT BOX
lightModal.alert = function(params)
{
    //create alert box
    var alertBox = $("<div />");
    alertBox.html(params.html);

    //set callback
    var callBack = function() { lightModal.closeParentModal(alertBox); if (params.callBack) params.callBack() };

    //add ok button
    var okButton = $("<div style='padding:30px 0 10px 0;text-align:center'><button>OK</button></div>");
    okButton.find("button").click(callBack);
    alertBox.append(okButton);

    //show modal
    lightModal.show({ width: params.width || 400, height: params.height || 200, html: alertBox });
}

//SUBMITS A FORM VIA AJAX
lightModal.submit = function(form)
{
    return lightModal.ajax({ type: "POST", url: form.action, data: $(form).serialize() });
};

//MAKES A GET REQUEST, AND EVALS THE RETURN VALUE
lightModal.get = function(url)
{
    return lightModal.ajax({ type: "GET", url: url });
};

//MAKES AN AJAX REQUEST
lightModal.ajax = function(params)
{
    $.ajax({
        type: params.type,
        url: params.url,
        data: params.data,
        success: params.onSuccess ? params.onSuccess() : function(js) { eval(js) },
        error: params.onError ? params.onError() : function(xhr, text, error) { lightModal.alert({ html: xhr.responseText, width: 600, height: 400 }) }
    });
    return false;
};
